using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Impact.Web.Foundation;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for the edit users view
    /// </summary>
    public partial class Users : PageBase
    {

        /// <summary>
        /// overridden to handling binding on this page
        /// </summary>
        protected override void Bind()
        {
            _userList.DataSource = Membership.GetAllUsers();
            _userList.DataBind();
        }

        /// <summary>
        /// event handler for the list of users repeater
        /// </summary>
        protected void UserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string userName = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "delete":
                    Membership.DeleteUser(userName, false);
                    break;

                case "password":
                    PasswordCommand(new Guid(e.CommandArgument.ToString()));
                    break;

                default:
                    throw new ArgumentException(string.Format("Command not found: {0}", e.CommandName));
            }

            Bind();

        }

        /// <summary>
        /// event handler for each item in the databound list
        /// </summary>
        protected void UserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
        }

        /// <summary>
        /// helper for when the user clicks the change password button in the repeater
        /// </summary>
        /// <param name="userKey">GUID for the user to change passwords</param>
        protected void PasswordCommand(Guid userKey)
        {
            MembershipUser currentUser = Membership.GetUser();
            if (currentUser.ProviderUserKey.Equals(userKey))
            {
                Redirect("ChangePassword.aspx");
            }

        }


    }
}
